package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @RequestMapping("/list")
    public String list(Model model){
        List<Department> departments = departmentService.listAll();
        model.addAttribute("departments",departments);
        return "department/list";
    }
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
            departmentService.dellect(id);
        }
        return "redirect:/department/list.do";
    }
    @RequestMapping("/input")
    public String input(Long id,Model model) {
        if (id != null) {
            Department department = departmentService.get(id);
            model.addAttribute("department",department);
        }
        return "department/input";
    }
    @RequestMapping("/saveOrUpadate")
    public String saveOrUpadate(Department department) {
        if (department.getId() == null) {
            departmentService.save(department);
        }else {
            departmentService.update(department);
        }
        return "redirect:/department/list.do";
    }
}

